// 游戏状态
let targetNumber;
let attempts;
let gameOver;
let minRange;
let maxRange;
let guessHistory;

// 初始化游戏
function initGame() {
    targetNumber = Math.floor(Math.random() * 100) + 1;
    attempts = 0;
    gameOver = false;
    minRange = 1;
    maxRange = 100;
    guessHistory = [];
    
    // 清空界面
    document.getElementById('guessInput').value = '';
    document.getElementById('message').textContent = '';
    document.getElementById('message').className = 'message';
    document.getElementById('attempts').textContent = '0';
    document.getElementById('range').textContent = '1 - 100';
    document.getElementById('historyList').innerHTML = '';
    document.getElementById('submitBtn').disabled = false;
    document.getElementById('guessInput').disabled = false;
    
    console.log('目标数字：', targetNumber); // 调试用
}

// 检查猜测
function checkGuess() {
    if (gameOver) return;
    
    const guessInput = document.getElementById('guessInput');
    const guess = parseInt(guessInput.value);
    const message = document.getElementById('message');
    
    // 验证输入
    if (isNaN(guess) || guess < 1 || guess > 100) {
        message.textContent = '请输入 1-100 之间的有效数字！';
        message.className = 'message error';
        return;
    }
    
    // 增加尝试次数
    attempts++;
    document.getElementById('attempts').textContent = attempts;
    
    // 添加到历史记录
    guessHistory.push(guess);
    updateHistory();
    
    // 检查猜测结果
    if (guess === targetNumber) {
        message.textContent = `🎉 恭喜你！猜对了！目标数字就是 ${targetNumber}！`;
        message.className = 'message success';
        gameOver = true;
        document.getElementById('submitBtn').disabled = true;
        document.getElementById('guessInput').disabled = true;
        
        // 添加庆祝动画
        celebrateWin();
    } else if (guess < targetNumber) {
        message.textContent = '太小了！再试试更大的数字。';
        message.className = 'message hint';
        minRange = Math.max(minRange, guess + 1);
        updateRange();
    } else {
        message.textContent = '太大了！再试试更小的数字。';
        message.className = 'message hint';
        maxRange = Math.min(maxRange, guess - 1);
        updateRange();
    }
    
    // 清空输入框并聚焦
    guessInput.value = '';
    guessInput.focus();
}

// 更新范围提示
function updateRange() {
    document.getElementById('range').textContent = `${minRange} - ${maxRange}`;
}

// 更新历史记录
function updateHistory() {
    const historyList = document.getElementById('historyList');
    const lastGuess = guessHistory[guessHistory.length - 1];
    
    const li = document.createElement('li');
    li.textContent = `第 ${guessHistory.length} 次：${lastGuess}`;
    
    if (lastGuess === targetNumber) {
        li.className = 'correct';
        li.textContent += ' ✓';
    } else if (lastGuess < targetNumber) {
        li.className = 'too-low';
        li.textContent += ' ↑';
    } else {
        li.className = 'too-high';
        li.textContent += ' ↓';
    }
    
    historyList.appendChild(li);
    
    // 自动滚动到最新记录
    historyList.scrollTop = historyList.scrollHeight;
}

// 重新开始游戏
function restartGame() {
    initGame();
}

// 胜利庆祝动画
function celebrateWin() {
    const container = document.querySelector('.container');
    container.style.animation = 'pulse 0.5s ease-in-out';
    
    setTimeout(() => {
        container.style.animation = '';
    }, 500);
}

// 键盘事件监听
document.addEventListener('keydown', function(event) {
    if (event.key === 'Enter' && !gameOver) {
        checkGuess();
    }
});

// 页面加载完成后初始化游戏
document.addEventListener('DOMContentLoaded', function() {
    initGame();
    
    // 聚焦输入框
    document.getElementById('guessInput').focus();
});

// 添加CSS动画
const style = document.createElement('style');
style.textContent = `
    @keyframes pulse {
        0% { transform: scale(1); }
        50% { transform: scale(1.05); }
        100% { transform: scale(1); }
    }
`;
document.head.appendChild(style);
